#include "mythread.h"

#include <iostream>
#include <thread>
namespace nilyang {
MyThread::MyThread() {}

int v = 1;
void critical_section(int change_v) {
  static std::mutex mtx;

  std::lock_guard<std::mutex> lock(mtx);

  // 执行竟态条件
  v = change_v;
  //离开次作用域mtx会自动释放
}

void func_thread() {
  std::thread t1(critical_section, 2), t2(critical_section, 3);
  t1.join();
  t2.join();

  std::cout << v << std::endl;
}

}  // namespace nilyang
